package com.ftg.learn.rbac.controller;

import com.ftg.learn.rbac.entity.Permission;
import com.ftg.learn.rbac.service.impl.PermissionServiceImpl;
import com.ftg.learn.util.BaseController;
import com.ftg.learn.util.ReturnData;
import io.swagger.annotations.Api;
import org.apiguardian.api.API;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kangjx
 * @since 2022-03-23
 */
@Api(tags = "权限管理列表")
@RestController
@RequestMapping("/rbac/permission")
public class PermissionController extends BaseController<Permission, PermissionServiceImpl> {

    @GetMapping("/showmenu")
    public ReturnData<Permission> showmenu(){
        //查到了表里面全部信息
        List<Permission> list = service.list();
        //对全部数据进行按格式
        ReturnData returnData = new ReturnData();
        returnData.setData(list);
        returnData.setCount((long)list.size());
        returnData.setCode(0);
        returnData.setMsg("success");
        return returnData;
    }

    @PutMapping("/updateForName")
    public boolean updateForName(@RequestParam int id,@RequestParam String title){
        return service.updateForName(id,title);
    }

    /**
     * 注意当前方法返回值
     * 用于生成角色授权时，所用到的菜单树
     * all : 全部菜单树
     * myself: 当前角色拥有的
     * @param rid 角色 id
     */
    @GetMapping("/showPermissionTree")
    public String showTree( int rid){
        return service.showPermissionTree(rid).toString();
    }


    /**
     * 用户分组
     * @param rid  角色编号
     * @param pids 权限编号
     * @return
     */
    @PostMapping("/saveForPermission")
    public Boolean saveForPermission(@RequestParam("rid") int rid,@RequestParam("pids") List<Integer> pids){
        System.out.println(rid);
        System.out.println(pids);
        return super.service.saveForPermission(rid,pids);
    }

}
